<?php
/**
 * Profile Controller.
 *  
 * @author Nguyen Hai Ha
 * @version 1.0
 */

class Member_ProfileController extends Zend_Controller_Action
{  
	protected $_auth;
	protected $_baseUrl;
	protected $_contextPath;
	protected $_utilities;
	protected $_errors;
	protected $_application;
	
	public function init(){
		$this->_baseUrl = $this->_request->getBaseURL ();
		$this->_contextPath = "member/profile";
		
		$this->view->assign ( "baseUrl", $this->_baseUrl );
		$this->view->assign ( "contextPath", $this->_contextPath );
		
		$this->_utilities = new Utilities();
		$this->view->assign("utilities", $this->_utilities);
		
		$this->_auth = Zend_Auth::getInstance()->getIdentity();
		$this->_application = new Zend_Session_Namespace(Constant::SESSION_NAMESPACE);
		
		$this->_errors = array ();
	}

	public function indexAction(){
		$this->_forward("view");
	}
	
	public function viewAction(){
		$user_id = $this->_auth->usr_id;
		$userModel = new UserModel();
		$account = $userModel->getUserById($user_id);
		$this->view->assign ( "account", $account );
		$this->render("view-profile");
	}
	
	public function updateAction()
	{
		if ($this->_request->isPost ()) {
			$request = $this->getRequest ();
			$userModel = new UserModel ( );
			
			$user_id = $this->_auth->usr_id;
			$user_mobile = $this->_auth->usr_mobile;
			$usr_fullname = $request->getPost ( "usr_fullname" );
			//$usr_birthday = $request->getPost ( "usr_birthday" );
			$usr_email = $request->getPost ( "usr_email" );
			$usr_modified_date = date ( 'Y-m-d H:m:s' );
			
			$f = new Zend_Filter_StripTags ( );
			$day = $f->filter ( $this->_request->getPost ( "day" ) );
			$month = $f->filter ( $this->_request->getPost ( "month" ) );
			$year = $f->filter ( $this->_request->getPost ( "year" ) );
			$usr_birthday = $year."-".$month."-".$day; 
			
//			if (strlen ( $usr_birthday ))
//				$usr_birthday = $this->_utilities->formatDateToDatabase ( $usr_birthday );
			
			$account = array ('usr_id'=>$user_id, 'usr_mobile' => $user_mobile, 'usr_fullname' => $usr_fullname, 'usr_birthday' => $usr_birthday, 'usr_email' => $usr_email, 'usr_modified_date' => $usr_modified_date );
			
			// Validate.
			if (! strlen ( $usr_fullname ))
				$this->_errors ['fullname_null'] = "Ban phai nhap ho va ten.";
			if (strlen ( $usr_email ))
				if (! $this->_utilities->checkemail ( $usr_email ))
					$this->_errors ['email_incorrect'] = "Dia chi email khong dung.";
			
			if (count ( $this->_errors )) {
				$account ['usr_birthday'] = $this->_utilities->formatdate ( $account ['usr_birthday'] );
				$this->view->assign ( "errors", $this->_errors );
				$this->view->assign ( "account", $account );
				$this->render ( "update-profile" );
				return;
			}
			
			$return = $userModel->update ( $account, $user_id );
			
			$this->_auth->usr_fullname = $usr_fullname;
			$this->_application->loggedUser = $usr_fullname;

			$this->_redirect ( $this->_contextPath . "/update-profile-success" );
			return;
		}
		
		$user_id = $this->_auth->usr_id;
		$userModel = new UserModel();
		$account = $userModel->getUserById($user_id);
		$this->view->assign ( "account", $account );
		$this->render ( "update-profile" );
	}
	
	public function updateProfileSuccessAction() {
	}
	
	public function changePasswordAction(){
		if ($this->_request->isPost ()) {
			$request = $this->getRequest ();
			$userModel = new UserModel ( );
			
			$old_password = $request->getPost ( "old_password" );
			$new_password = $request->getPost ( "new_password" );
			$confirm_new_password = $request->getPost ( "confirm_new_password" );
			
			$user_mobile = $this->_auth->usr_mobile;
			$salt = 'klmn01234opqrst';
			
			if ($new_password != $confirm_new_password) {
				$this->_errors ['confirm_password_incorrect'] = "Confirm password khong dung.";
			} else if (strlen ( $new_password ) < 6)
				$this->_errors ['password_length_incorrect'] = "Password phai lon hon 6 ky tu.";
			
			if (strlen($old_password)){
				
				$check = $userModel->checkPassword ( $user_mobile, md5(md5 ( $old_password.$salt )) );
				if (! $check) 
					$this->_errors ['old_password_incorrect'] = "Old password khong dung.";
			} else {
				$this->_errors['old_password_null'] = "Ban phai nhap old password.";
			}
			
			if (count ( $this->_errors )) {
				$this->view->assign ( "errors", $this->_errors );
				$this->render ( "change-password" );
				return;
			} else {
				$usr_id = $this->_auth->usr_id;
				$userModel->update ( array ('usr_password' => md5(md5 ( $new_password.$salt )) ), $usr_id );
				$this->_redirect ( $this->_contextPath . "/change-password-success" );
			}
			return;
		}
		
		$this->render ( "change-password" );
	}
	
	public function changePasswordSuccessAction() {
	}
	
}//End class.